%% Based on a TeXnicCenter-Template by Tino Weinkauf.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% HEADER
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\documentclass[letterpaper,twoside,10pt]{report}
% Alternative Options:
%	Paper Size: a4paper / a5paper / b5paper / letterpaper / legalpaper / executivepaper
% Duplex: oneside / twoside
% Base Font Size: 10pt / 11pt / 12pt


%% Language %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage[USenglish]{babel} %francais, polish, spanish, ...
\usepackage[T1]{fontenc}
\usepackage[ansinew]{inputenc}
\usepackage{lmodern} %Type1-font for non-english texts and characters


%% Packages for Graphics & Figures %%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{graphicx} %%For loading graphic files
%\usepackage{subfig} %%Subfigures inside a figure
%\usepackage{tikz} %%Generate vector graphics from within LaTeX

%% Please note:
%% Images can be included using \includegraphics{filename}
%% resp. using the dialog in the Insert menu.
%% 
%% The mode "LaTeX => PDF" allows the following formats:
%%   .jpg  .png  .pdf  .mps
%% 
%% The modes "LaTeX => DVI", "LaTeX => PS" und "LaTeX => PS => PDF"
%% allow the following formats:
%%   .eps  .ps  .bmp  .pict  .pntg


%% Math Packages %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{amsfonts}

%% Line Spacing %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\usepackage{setspace}
%\singlespacing        %% 1-spacing (default)
%\onehalfspacing       %% 1,5-spacing
%\doublespacing        %% 2-spacing


%% Other Packages %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\usepackage{a4wide} %%Smaller margins = more text per page.
%\usepackage{fancyhdr} %%Fancy headings
%\usepackage{longtable} %%For tables, that exceed one page
\usepackage{hyperref}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Remarks
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% TODO:
% 1. Edit the used packages and their options (see above).
% 2. If you want, add a BibTeX-File to the project
%    (e.g., 'literature.bib').
% 3. Happy TeXing!
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Options / Modifications
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%\input{options} %You need a file 'options.tex' for this
%% ==> TeXnicCenter supplies some possible option files
%% ==> with its templates (File | New from Template...).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% USER DEFINED COMMAND 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newtheorem{theorem}{Theorem}[section]
\newtheorem{lemma}[theorem]{Lemma}
\newtheorem{proposition}[theorem]{Proposition}
\newtheorem{corollary}[theorem]{Corollary}

%\newenvironment{proof}[1][Proof]{\begin{trivlist}
%\item[\hskip \labelsep {\bfseries #1}]}{\end{trivlist}}
\newenvironment{definition}[1][Definition]{\begin{trivlist}
\item[\hskip \labelsep {\bfseries #1}]}{\end{trivlist}}
\newenvironment{example}[1][Example]{\begin{trivlist}
\item[\hskip \labelsep {\bfseries #1}]}{\end{trivlist}}
\newenvironment{remark}[1][Remark]{\begin{trivlist}
\item[\hskip \labelsep {\bfseries #1}]}{\end{trivlist}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% DOCUMENT
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}

\pagestyle{empty} %No headings for the first pages.


%% Title Page %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% ==> Write your text here or include other files.

%% The simple version:
\title{LPCVD User and Developer Mannual}
\author{Xinyu Zhang, Jianqiao Huang, Gangshi Hu}
%\date{} %%If commented, the current date is used.
\maketitle

%% The nice version:
%\input{titlepage} %%You need a file 'titlepage.tex' for this.
%% ==> TeXnicCenter supplies a possible titlepage file
%% ==> with its templates (File | New from Template...).


%% Inhaltsverzeichnis %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\tableofcontents %Table of contents
\cleardoublepage %The first chapter should start on an odd page.

\pagestyle{plain} %Now display headings: headings / fancy / ...



%% Chapters %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% ==> Write your text here or include other files.

%\input{intro} %You need a file 'intro.tex' for this.


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\chapter{Get LPCVD}\label{ch:install}
\section{Download the LPCVD library source code}
Assume you want to download the code to your local folder called \verb|code|
\begin{verbatim}
svn checkout https://lpcvd.googlecode.com/svn/trunk/ code
\end{verbatim}

\section{Install Third Party Code}
Three libraries are needed to run \verb|LPCVD|:
\begin{description}
	\item[IPOPT] Internal point optimizer, used to solve MPC problem. Make sure you have a copy of Ipopt-3.8.0-win32-dll, which is a pre-compiled version of IPOPT.
	\item[GSL] GNU scientific library. Needed to generate Gaussian distribution random variable.
	\item[FFTW] Fast fourier transform library
\end{description}
Download these library and put them in a folder, e.g., \verb|ThirdPartyCode|. Both IPOPT and GSL are distributed as compiled library on Windows.

Assume your copy is located at \verb|ThirdPartyCode|, then you need to add to the environment variable PATH
\begin{verbatim}
PATH=PATH;$IPOPT_DIR\lib\win32\release\
GSL
\end{verbatim}

Make sure you have the gsl directory under the folder \verb|ThirdPartyCode|
 In the folder \verb|gsl/lib| there should be the following files:
\begin{verbatim}
cblas.lib
clbas_d.lib
gsl.lib
gsl_d.lib
\end{verbatim}
On Linux
On Linux, we need to compile and install IPOPT and GSL library.

IPOPT

GSL

3. Set up Matlab path
4. Write your simulation program
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\chapter{Run Simulations with LPCVD}\label{ch:run_simulation}
\section{Working flow overview}
\begin{enumerate}
	\item Make a list of figures that we need to obtain. The principle is think about the final products, the figures, from the very beginning. For each figure, list the data sets needed.
	\item Go through the figure list and data sets list, see if some data sets are shared by multiple figures.
	\item For each data set, create a project (or folder).
  \item Run open-loop simulations
  \item Do fitting and create a fitting models
  \item Verify the fitting model
  \item Run closed-loop simulation with 
\end{enumerate}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{A step-by-step guide}
\subsection{Setup the simulation folder}
The following directory structures is one option on how to organize your simulations. The following directory structures is one option on how to organize your simulations.

Assume you have put the LPCVD code in folder \verb|code|, now you need to create a folder for your simulation files and data, e.g., \verb|Simulations|. Here we give an example of the structure within in the folder \verb|Simulations|
\begin{verbatim}
code/
Simulations/
  Simulations.sln (Visual studio solution file)
  Doc/
    Simulation_plan.tex
    Simulation_report.tex
  Open_loop/
  Close_loop/
  Fitting_models/
\end{verbatim}

\begin{remark}
It is recommended that the simulation plan and simulation report are written in because 1) it's easier to find the figure files later when writing the paper -- just look at the tex source code. If Word is used, I need to manully find the original file. Besides, I need to create both EPS and JPG format; 2) easier to manage via SVN.
\end{remark}
You can mannuly create this these folders or use a the script \verb|setup_simulation.sh| (or \verb|setup_simulation.bat|) in at \verb|code/examples|.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Run open loop simulations}
An example of open loop simulation
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Fitting}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Verify the fitting}
\begin{itemize}
	\item To before the verification steps, make sure you have an fitting model in a subfolder in \verb|Fitting_models|, e.g., \verb|Fitting_1|.
	\item Create two open-loop simulations in the \verb|Open_loop| folder, one with SPDE and the other with the controller model. For example, \verb|SPDE_1| and \verb|Controller_1|.
\end{itemize}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Run closed--loop simulations}
1) Copy the example simulation files to a new folder
\begin{verbatim}
mkdir CURRENT_SIMULATION_FOLDER
cd ~/LPCVD/code/examples/close_loop/KMC_plant/
cp *.cpp make *.py CURRENT_SIMULATION_FODLER
\end{verbatim}

Remark: \textbf{Don't use the following command because the hidden folder .SVN will also be copied.}
\begin{verbatim}
cp -r ~/LPCVD/code/examples/close_loop/KMC_plant/ CURRENT_SIMULATION_FOLDER_NAME
\end{verbatim}

2) Modify the simulation files
File:\verb|makefile|, change the following lines:
\begin{verbatim}
SIMULATION = ./simulation.cpp\\ /LPCVD/Simulations3/FittingModels/Fitting6_Jianqiao_r2/MPC_EW1D_fitting_model_6.cpp
\end{verbatim}

File:\verb|simulation_master.py|:
\begin{verbatim}
cp -r ~/LPCVD/code/examples/close_loop/KMC_plant/ CURRENT_SIMULATION_FOLDER_NAME
\end{verbatim}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Run open--loop simulations}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Having troubles?}
It is quite common you will have some trouble in your first try.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\chapter{For Developers}




%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% BIBLIOGRAPHY AND OTHER LISTS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% A small distance to the other stuff in the table of contents (toc)
%\addtocontents{toc}{\protect\vspace*{\baselineskip}}

%% The Bibliography
%% ==> You need a file 'literature.bib' for this.
%% ==> You need to run BibTeX for this (Project | Properties... | Uses BibTeX)
%\addcontentsline{toc}{chapter}{Bibliography} %'Bibliography' into toc
%\nocite{*} %Even non-cited BibTeX-Entries will be shown.
%\bibliographystyle{alpha} %Style of Bibliography: plain / apalike / amsalpha / ...
%\bibliography{literature} %You need a file 'literature.bib' for this.

%% The List of Figures
%\clearpage
%\addcontentsline{toc}{chapter}{List of Figures}
%\listoffigures

%% The List of Tables
%\clearpage
%\addcontentsline{toc}{chapter}{List of Tables}
%\listoftables


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% APPENDICES
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\appendix
%% ==> Write your text here or include other files.

%\input{FileName} %You need a file 'FileName.tex' for this.


\end{document}

